package com.megvii.koala.data;

import android.content.Context;

import com.megvii.koala.data.bean.LoginBean;
import com.megvii.koala.data.bean.StaffBean;
import com.megvii.koala.data.bean.VisitorBean;
import com.megvii.koala.data.comparetor.PinyinComparator;
import com.megvii.koala.data.manager.ClockInManager;
import com.megvii.koala.data.manager.CompanyManager;
import com.megvii.koala.data.manager.HistoryManager;
import com.megvii.koala.data.manager.NetLockerManager;
import com.megvii.koala.data.manager.StaffManager;
import com.megvii.koala.data.manager.UserInfoManager;
import com.megvii.koala.data.manager.VisitorHistoryManager;
import com.megvii.koala.data.manager.VisitorManager;
import com.megvii.koala.data.model.LoginModel;
import com.megvii.koala.guard.BoxManager;
import com.megvii.koala.net.KoalaHttpManager;

//先不考虑多线程调用
public class GlobalData {
    private static GlobalData instance = null;

    public StaffManager staffManager = new StaffManager(new PinyinComparator<StaffBean>());

    /**
     * 这里先默认用PinyinComparator，如果有app需要用别的Comparator请自行setComparator
     */
    public VisitorManager visitorManager = new VisitorManager(new PinyinComparator<VisitorBean>());

    public KoalaHttpManager httpManager;

    public KoalaHttpManager checkInHttpManager;

    public UserInfoManager userInfoManager = new UserInfoManager();

    public CompanyManager companyManager = new CompanyManager();

    public ClockInManager clockInManager = new ClockInManager();

    public HistoryManager historyManager = new HistoryManager();

    public BoxManager boxManager;

    public NetLockerManager netLockerManager = null;

    public VisitorHistoryManager visitorHistoryManager = new VisitorHistoryManager(visitorManager);

    public LoginBean loginBean;

    private Object releaseFlag = null;

    /**
     * TODO 不许外部改变这个的引用，需要写个get函数
     */
    private LoginModel login = new LoginModel();

    public GlobalData() {
        super();
    }

    public static boolean hasInstance() {
        return instance != null;
    }

    public static void setInstance(GlobalData instance) {
        GlobalData.instance = instance;
    }

    public static GlobalData instance() {
        if (instance == null) {
            instance = new GlobalData();
        }
        return instance;
    }

    //    public static KoalaIndividualApplication getAppliction()
    //    {
    //        // TODO Auto-generated method stub
    //        Class localClass;
    //        try
    //        {
    //            localClass = Class.forName("android.app.ActivityThread");
    //            Method getapplication = localClass.getMethod("currentApplication");
    //            Object myapplication = getapplication.invoke(localClass);
    //            KoalaIndividualApplication application = (KoalaIndividualApplication) myapplication;
    //            //System.out.println("====ok===== 获取包名:" + application.getPackageName());
    //            return application;
    //        }
    //        catch (ClassNotFoundException e)
    //        {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        catch (NoSuchMethodException e)
    //        {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        catch (IllegalArgumentException e)
    //        {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        catch (IllegalAccessException e)
    //        {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        catch (InvocationTargetException e)
    //        {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        return null;
    //    }

    public void init(Context applicationContext, String userAgent) {
        if (releaseFlag == null) {
            //
            releaseFlag = new Object();
            //
            httpManager = new KoalaHttpManager(applicationContext);
            httpManager.setUserAgent(userAgent);
            checkInHttpManager = new KoalaHttpManager(applicationContext);
            userInfoManager.setUrlMaker(login);
            companyManager.setUrlMaker(login);
            clockInManager.setUrlMaker(login);
            staffManager.setUrlMaker(login);
            visitorManager.setUrlMaker(login);
            visitorHistoryManager.setUrlMaker(login);
            boxManager = new BoxManager();
            boxManager.init();
        }
    }

    public void initWithNetLocker(Context applicationContext, String userAgent) {
        init(applicationContext, userAgent);
        netLockerManager = new NetLockerManager();
        netLockerManager.init(applicationContext);
    }

    public boolean isCleared() {
        return releaseFlag == null;
    }

    public void release() {
        releaseFlag = null;
        clearData();
        if (boxManager != null) {
            boxManager.release();
            boxManager = null;
        }
        if (netLockerManager != null) {
            netLockerManager.release();
            netLockerManager = null;
        }
        if (httpManager != null) {
            httpManager.release();
        }
        if (checkInHttpManager != null) {
            checkInHttpManager.release();
        }
    }

    public void clearData() {
        if (boxManager != null)
            boxManager.clear();
        if (staffManager != null)
            staffManager.clear();
        if (visitorManager != null)
            visitorManager.clear();
        if (userInfoManager != null)
            userInfoManager.clear();
        if (companyManager != null)
            companyManager.clear();
        if (clockInManager != null)
            clockInManager.clear();
        if (historyManager != null)
            historyManager.clear();
        if (visitorHistoryManager != null)
            visitorHistoryManager.clear();
    }

    public LoginModel getLogin() {
        return login;
    }
}
